﻿#if SILVERLIGHT
#region File and License Information
/*
<File>
	<License>
		Copyright © 2009 - 2017, Daniel Vaughan. All rights reserved.
		This file is part of Calcium (http://calciumsdk.net), 
		which is released under the MIT License.
		See file /Documentation/License.txt for details.
	</License>
	<CreationDate>2010-08-18 18:36:28Z</CreationDate>
</File>
*/
#endregion

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Outcoder.InversionOfControl;

namespace Outcoder.Services.Implementation
{
	public class NavigationServiceWrapper : INavigationService
	{
		readonly NavigationService navigationService;

		public NavigationServiceWrapper(NavigationService navigationService)
		{
			this.navigationService = ArgumentValidator.AssertNotNull(navigationService, "navigationService");
		}

		public void GoBack()
		{
			navigationService.GoBack();
		}

		public bool CanGoBack
		{
			get
			{
				return navigationService.CanGoBack;
			}
		}

		public void Navigate(Uri source)
		{
			ArgumentValidator.AssertNotNull(source, "source");
			navigationService.Navigate(source);
		}

		public void Navigate(string relativeUrl)
		{
			ArgumentValidator.AssertNotNull(relativeUrl, "relativeUrl");
			navigationService.Navigate(new Uri(relativeUrl, UriKind.Relative));
		}

		public Uri Source
		{
			get
			{
				return navigationService.Source;
			}
		}
	}

	public class FrameNavigationService : INavigationService
	{
		Frame frameUseProperty;

		Frame Frame
		{
			get
			{
				if (frameUseProperty == null)
				{
					frameUseProperty = (Frame)Application.Current.RootVisual;
#if !WINDOWS_PHONE
					if (frameUseProperty == null)
					{
						throw new InvalidOperationException(
							@"The application does not contain a root frame. To allow navigation throughout your application set the RootVisual of your application to a Frame instance, like so: 
void Application_Startup(object sender, StartupEventArgs e)
		{
			Frame frame = new Frame();
			RootVisual = frame;
			frame.Navigate(new Uri(""/Views/FirstView.xaml"", UriKind.Relative));
		}");
					}
#endif
				}
				return frameUseProperty;
			}
		}

		public FrameNavigationService(Frame frame)
		{
			frameUseProperty = ArgumentValidator.AssertNotNull(frame, "frame");
		}

		[InjectDependencies]
		public FrameNavigationService()
		{
			/* Intentionally left blank. */
		}

		public void GoBack()
		{
			Frame.GoBack();
		}

		public bool CanGoBack
		{
			get
			{
				return Frame.CanGoBack;
			}
		}

		public void Navigate(Uri source)
		{
			ArgumentValidator.AssertNotNull(source, "source");
			Frame.Navigate(source);
		}

		public void Navigate(string relativeUrl)
		{
			ArgumentValidator.AssertNotNull(relativeUrl, "relativeUrl");
			Frame.Navigate(new Uri(relativeUrl, UriKind.Relative));
		}

		public Uri Source
		{
			get
			{
				return Frame.Source;
			}
		}
	}
}
#endif